package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.ClassTimeSet;
import com.xmy.cultivate.entity.Organization;
import com.xmy.cultivate.service.IClassTimeSetService;
import com.xmy.cultivate.service.IOrganizationService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author min
 * @since 2022-09-21
 */
@RestController
@RequestMapping("/api/classTimeSet")
@Scope("prototype")
public class ClassTimeSetController extends BaseController{

    @Autowired
    @Lazy
    IClassTimeSetService iClassTimeSetService;

    @Autowired
    @Lazy
    IOrganizationService iOrganizationService;

    @GetMapping()
    public Result findAll(@RequestParam(value = "schoolId",defaultValue = "") String schoolId,
                          @RequestParam(value = "lessonType",defaultValue = "") String lessonType,
                          @RequestParam(value = "type",defaultValue = "") String type){
        QueryWrapper<ClassTimeSet> classTimeSetQueryWrapper = new QueryWrapper<>();
        classTimeSetQueryWrapper.eq(StringUtils.isNotBlank(lessonType),"lesson_type",lessonType);
        if (StringUtils.isNotBlank(schoolId)){
            classTimeSetQueryWrapper.and(i->i.or(j->j.eq("school_id",schoolId)).or(j->j.eq("school_id",0)));
        }else {
            classTimeSetQueryWrapper.and(i->i.or(j->j.in("school_id",this.baseSchoolIdList)).or(j->j.eq("school_id",0)));
        }
        classTimeSetQueryWrapper.eq(StringUtils.isNotBlank(type),"type",type);
        classTimeSetQueryWrapper.groupBy("start_time","end_time","type");
        classTimeSetQueryWrapper.orderByAsc("start_time");
        List<ClassTimeSet> classTimeSetList = iClassTimeSetService.list(classTimeSetQueryWrapper);
        for (ClassTimeSet classTimeSet:classTimeSetList){
            if (!classTimeSet.getSchoolId().equals(0L)){
                Organization organization = iOrganizationService.getById(classTimeSet.getSchoolId());
                if (organization!=null){
                    classTimeSet.setSchoolName(organization.getName());
                }
            }else {
                classTimeSet.setSchoolName("全部校区");
            }
        }


        return new Result(Code.OK,new PageResult(classTimeSetList,(long)classTimeSetList.size(),1L));
    }

    @PostMapping()
    public Result save(@RequestBody ClassTimeSet classTimeSet){
        System.out.println(classTimeSet);
        iClassTimeSetService.save(classTimeSet);
        //System.out.println(course);
        return new Result(Code.OK);
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable(value = "id") String id,@RequestBody ClassTimeSet classTimeSet){
        iClassTimeSetService.updateById(classTimeSet);
        return new Result(Code.OK);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable(value = "id") Long id){
        System.out.print("id:"+id);
        boolean rs =  iClassTimeSetService.removeById(id);
        return new Result(rs?Code.OK:Code.ERROR);
    }
}
